package br.com.sgdc.view.util;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class CriptoUtil {
	private byte[] seed = { 47, -51, 70, -82, 61, -122, -119, -36 };

	private SecretKey secretKey;
	private Cipher cipher;

	public CriptoUtil() throws Exception {
		KeyGenerator keyGen = KeyGenerator.getInstance("DES");
		SecureRandom secRam = new SecureRandom(seed);
		keyGen.init(56, secRam);

		secretKey = keyGen.generateKey();
		cipher = Cipher.getInstance("DES");
	}

	public String encryptData(String data) throws Exception {
		byte[] byteData = data.getBytes("utf-8");

		cipher.init(Cipher.ENCRYPT_MODE, secretKey);
		byte[] byteCript = cipher.doFinal(byteData);

		String strCript = ConvertUtil.byteArrayToHexString(byteCript);
		return strCript;
	}

	public String decryptData(String strCript) throws Exception {
		byte[] byteCript = ConvertUtil.hexStringToByteArray(strCript);

		cipher.init(Cipher.DECRYPT_MODE, secretKey);
		byte[] byteData = cipher.doFinal(byteCript);

		return new String(byteData, "utf-8");
	}
}
